package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.atguigu.gulimall.common.utils.PageUtil;
import com.atguigu.gulimall.common.utils.TableDataInfo;
import com.atguigu.gulimall.common.valid.AddGroup;
import com.atguigu.gulimall.product.utils.UnicodeUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.gulimall.common.utils.R;


/**
 * 品牌
 *
 * @author yangni
 * @email 1607401968@qq.com
 * @date 2022-01-22 16:34:30
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object>params,@RequestParam("PageNum") int PageNum,@RequestParam("PageSize") int PageSize) {
//        PageUtils page = brandService.queryPage(params);
        List<BrandEntity> list = brandService.list(new LambdaQueryWrapper<>(BrandEntity.class)
                .like(ObjectUtil.isNotEmpty(params.get("key")),BrandEntity::getName,params.get("key")));
        TableDataInfo<BrandEntity> page = PageUtil.getPage(PageNum,PageSize , list);
        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult bindResult*/) {
//        if (bindResult.hasErrors()) {
//            Map<String, String> map = new HashMap<>();
//            //1.获取校验的错误
//            for (ObjectError allError : bindResult.getAllErrors()) {
//                //错误提示消息
//                String defaultMessage = allError.getDefaultMessage();
//                String objectName = allError.getObjectName();
//                map.put(objectName, defaultMessage);
//            }
//            return R.error(400, "提交的数据不合法").put("data", map);
//        } else {
//
//            brandService.save(brand);
//        }
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@RequestBody BrandEntity brand) {
//        brandService.updateById(brand);
        brandService.updateDetail(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

    @RequestMapping("test")
    public R test() {
        String s = "探索浩瀚宇宙，发展航天事业，建设航天强国，是我们不懈追求的航天梦!";
        String s1 = UnicodeUtils.toUnicode(s);
        String convert = UnicodeUtils.unicodeDecode(s1);
        return R.ok(convert);
    }

}
